package com.geostar.gmp.mina;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.geostar.gmp.global.ConfigValue;
import com.geostar.gmp.model.ProcessModel;
import com.geostar.gmp.utils.ReadXMLUtils;

/**
 * 初始化XML文件中需要监控的进程
 * 
 * @author yesong
 */
public class MInitProcessXML {

	private static Logger logger = LoggerFactory
			.getLogger(MInitProcessXML.class);

	/**
	 * 将XML文件中需要监控的进程对象装换为ProcessModel集合
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<ProcessModel> initProcessModel(String ip) {
		List<ProcessModel> pmList = new ArrayList<ProcessModel>();
		try {
			ReadXMLUtils.loadXML(ConfigValue.MPROCESS_XML);
			Element ip_element = ReadXMLUtils.readElementsById(ip);
			if (ip_element != null) {
				List<Element> pros = ip_element.elements("pro");
				if (pros != null && pros.size() > 0) {
					ProcessModel pm = new ProcessModel();
					for (Element pro : pros) {
						if (StringUtils.isNotBlank(pro.getTextTrim())) {
							pm.setIp(pro.getTextTrim());
							pmList.add(pm);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("初始化XML文件中需要监控的进程过程异常");
		} finally {
			ReadXMLUtils.closeInputStream();
		}
		return pmList;
	}
}
